<template>
  <div class="login-container">
    <!-- 左侧品牌介绍区域 (70%) -->
    <div class="brand-section">
      <div class="brand-content">
        <div class="title-group">
          <h1 class="main-title" style="margin-top: 20px">企业级OA中台办公系统</h1>
          <div class="title-underline"></div>
        </div>

        <p class="slogan">高效 · 安全 · 智能 · 协同</p>

        <div class="feature-list">
          <div class="feature-item">
            <div class="feature-icon">
              <i class="fas fa-chart-line"></i>
            </div>
            <div class="feature-text">
              <h3>数据驱动决策</h3>
              <p>多维度数据分析，助力管理层精准决策</p>
            </div>
          </div>

          <div class="feature-item">
            <div class="feature-icon">
              <i class="fas fa-users-cog"></i>
            </div>
            <div class="feature-text">
              <h3>高效协同办公</h3>
              <p>打破部门壁垒，实现信息共享与高效协作</p>
            </div>
          </div>

          <div class="feature-item">
            <div class="feature-icon">
              <i class="fas fa-shield-alt"></i>
            </div>
            <div class="feature-text">
              <h3>安全可靠保障</h3>
              <p>多层次安全防护，保障企业数据安全</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧登录区域 (30%) -->
    <div class="login-section">
      <div class="login-form-container">
        <h2 class="login-title">系统登录</h2>
        <p class="login-subtitle">欢迎回来，请输入您的账号信息</p>

        <div class="input-group">
          <label for="phone">用户名/手机号</label>
          <div class="input-wrapper">
            <i class="fas fa-user"></i>
            <input
              id="phone"
              type="text"
              v-model="phone"
              placeholder="请输入用户名/手机号"
            />
          </div>
        </div>

        <div class="input-group">
          <label for="password">密码</label>
          <div class="input-wrapper">
            <i class="fas fa-mobile-alt"></i>
            <input
              id="password"
              type="password"
              v-model="password"
              placeholder="请输入密码"
            />
          </div>
        </div>

        <div class="options-row">
          <label class="remember-me">
            <input type="checkbox" v-model="rememberMe" />
            <span>7天免登录</span>
          </label>
          <a href="#" class="forgot-link">忘记密码?</a>
        </div>

        <button class="login-btn" @click="handleLogin">
          <span>登录系统</span>
          <i class="fas fa-arrow-right"></i>
        </button>

        <div class="register-link-container">
          <span>还没有账号?</span>
          <a @click.prevent="register" class="register-link">立即注册</a>
        </div>
      </div>

      <div class="login-footer">
        <p>© {{ currentYear }} 企业级OA中台办公系统 版权所有</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { Login } from "../utils/api"; // 导入Login接口
import { useCounterStore } from "@/stores/counter"; // 导入store

const router = useRouter();
const store = useCounterStore(); // 使用store
const phone = ref("");
const password = ref("");
const rememberMe = ref(false);
const currentYear = new Date().getFullYear();

const handleLogin = async () => {
  try {
    const response = await Login({
      phone: phone.value,
      password: password.value,
    });

    console.log("完整响应对象:", response);
    console.log("登录接口返回的token:", response.token);
    console.log("登录接口返回的完整数据:", JSON.stringify(response));

    // 检查response中的所有字段
    console.log("response中的所有字段:");
    for (const key in response) {
      console.log(`${key}: ${JSON.stringify(response[key])}`);
    }

    if (response && typeof response.token === "string" && response.token.length > 0) {
      // 保存用户数据到localStorage
      localStorage.setItem("data", JSON.stringify(response));
      localStorage.setItem("token", response.token);

      // 调试输出用户数据
      console.log("保存到localStorage的数据:", JSON.stringify(response));

      // 根据用户角色过滤路由
      const userRole = response.department || "员工"; // 使用department字段，如果不存在则默认为员工
      console.log("用户角色 (department):", userRole);

      // 调用store中的方法过滤路由
      store.filterRoutesByRole(userRole);

      router.push("/");
    } else {
      const message = (response && response.message) || "登录失败，请检查手机号和密码";
      alert(message);
    }
  } catch (error) {
    console.error("登录请求出错:", error);
    alert("登录请求失败，请稍后再试");
  }
};

const register = () => {
  router.push("/register");
};
</script>

<style scoped>
@import url("https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;500;600;700&display=swap");
@import url("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css");

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login-container {
  display: flex;
  height: 100vh;
  width: 100%;
  font-family: "Nunito", sans-serif;
}

/* 左侧品牌介绍区域 (70%) */
.brand-section {
  width: 70%;
  background: linear-gradient(135deg, #112b4a, #1a4173);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

/* 背景动画元素 */
.brand-section::before,
.brand-section::after {
  content: "";
  position: absolute;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.05) 0%, transparent 70%);
  border-radius: 50%;
}

.brand-section::before {
  width: 800px;
  height: 800px;
  top: -400px;
  left: -200px;
  animation: pulse 8s infinite alternate;
}

.brand-section::after {
  width: 600px;
  height: 600px;
  bottom: -300px;
  right: -100px;
  animation: wave 12s infinite ease-in-out;
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 0.3;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.5;
  }
  100% {
    transform: scale(1);
    opacity: 0.3;
  }
}

@keyframes wave {
  0% {
    transform: translateY(0) rotate(0deg);
  }
  25% {
    transform: translateY(-30px) rotate(90deg);
  }
  50% {
    transform: translateY(0) rotate(180deg);
  }
  75% {
    transform: translateY(30px) rotate(270deg);
  }
  100% {
    transform: translateY(0) rotate(360deg);
  }
}

.brand-content {
  position: relative;
  z-index: 2;
  max-width: 700px;
  color: white;
}

.title-group {
  margin-bottom: 30px;
  animation: fadeInUp 1s ease;
}

.main-title {
  font-size: 3.2rem;
  font-weight: 700;
  margin-bottom: 15px;
  letter-spacing: 1px;
  line-height: 1.2;
}

.title-underline {
  width: 100px;
  height: 4px;
  background: linear-gradient(to right, #ffffff, rgba(255, 255, 255, 0.3));
  animation: widthExtend 1.5s ease;
}

@keyframes widthExtend {
  from {
    width: 0;
  }
  to {
    width: 100px;
  }
}

.slogan {
  font-size: 1.5rem;
  opacity: 0.9;
  margin-bottom: 60px;
  letter-spacing: 2px;
  animation: fadeInUp 1.2s ease;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  gap: 20px;
  animation: fadeInRight 1.4s ease;
  transition: all 0.3s ease;
  position: relative;
  padding: 15px;
  border-radius: 10px;
}

.feature-item::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  opacity: 0;
  transition: all 0.3s ease;
}

.feature-item:hover {
  transform: translateY(-5px);
}

.feature-item:hover::before {
  opacity: 1;
}

.feature-item:nth-child(2) {
  animation-delay: 0.2s;
}

.feature-item:nth-child(3) {
  animation-delay: 0.4s;
}

.feature-icon {
  font-size: 1.8rem;
  color: #ffffff;
  background: rgba(255, 255, 255, 0.1);
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.feature-item:hover .feature-icon {
  background: rgba(255, 255, 255, 0.2);
  transform: rotate(5deg);
}

.feature-text h3 {
  font-size: 1.3rem;
  margin-bottom: 8px;
  font-weight: 600;
}

.feature-text p {
  font-size: 1rem;
  opacity: 0.8;
  line-height: 1.5;
}

/* 右侧登录区域 (30%) */
.login-section {
  width: 30%;
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 2rem;
  box-shadow: -5px 0 30px rgba(0, 0, 0, 0.1);
}

.login-form-container {
  width: 100%;
  max-width: 350px;
  animation: slideInUp 0.8s ease;
}

.login-title {
  font-size: 1.8rem;
  font-weight: 700;
  color: #112b4a;
  margin-bottom: 0.5rem;
}

.login-subtitle {
  color: #64748b;
  margin-bottom: 2rem;
  font-size: 0.9rem;
}

.input-group {
  margin-bottom: 1.2rem;
}

.input-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
  color: #334155;
  font-size: 0.85rem;
}

.input-wrapper {
  display: flex;
  align-items: center;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 0 0.8rem;
  background-color: white;
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
}

.input-wrapper::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: #112b4a;
  transition: all 0.3s ease;
  z-index: -1;
}

.input-wrapper:focus-within {
  border-color: #112b4a;
  box-shadow: 0 0 0 2px rgba(17, 43, 74, 0.1);
}

.input-wrapper:focus-within::after {
  width: 100%;
  left: 0;
}

.input-wrapper i {
  color: #94a3b8;
  margin-right: 0.75rem;
  font-size: 0.9rem;
}

.input-wrapper input {
  width: 100%;
  padding: 0.7rem 0;
  border: none;
  outline: none;
  font-size: 0.9rem;
  color: #334155;
}

.options-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.2rem;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  color: #64748b;
  cursor: pointer;
}

.remember-me input {
  accent-color: #112b4a;
}

.forgot-link {
  color: #112b4a;
  font-size: 0.8rem;
  text-decoration: none;
  transition: all 0.3s ease;
}

.forgot-link:hover {
  text-decoration: underline;
}

.login-btn {
  width: 100%;
  padding: 0.8rem;
  background-color: #112b4a;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: "";
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: 0.5s;
}

.login-btn i {
  opacity: 0;
  transform: translateX(-10px);
  transition: all 0.3s ease;
}

.login-btn:hover {
  background-color: #193b63;
  box-shadow: 0 5px 15px rgba(17, 43, 74, 0.3);
}

.login-btn:hover::before {
  left: 100%;
}

.login-btn:hover i {
  opacity: 1;
  transform: translateX(0);
}

.register-link-container {
  text-align: center;
  margin-top: 1.2rem;
  font-size: 0.8rem;
  color: #64748b;
}

.register-link {
  color: #112b4a;
  font-weight: 600;
  text-decoration: none;
  margin-left: 0.25rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.register-link:hover {
  text-decoration: underline;
}

.login-footer {
  margin-top: 2rem;
  text-align: center;
  color: #94a3b8;
  font-size: 0.75rem;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(40px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .login-form-container {
    max-width: 90%;
  }

  .main-title {
    font-size: 2.5rem;
  }

  .slogan {
    font-size: 1.2rem;
    margin-bottom: 40px;
  }

  .feature-text h3 {
    font-size: 1.1rem;
  }

  .feature-text p {
    font-size: 0.9rem;
  }
}

@media (max-width: 992px) {
  .login-container {
    flex-direction: column;
  }

  .brand-section,
  .login-section {
    width: 100%;
  }

  .brand-section {
    padding: 40px 20px;
  }

  .login-section {
    padding: 1.5rem;
  }

  .main-title {
    font-size: 2rem;
  }

  .feature-list {
    gap: 20px;
  }
}

@media (max-width: 576px) {
  .brand-section {
    padding: 30px 15px;
  }

  .login-section {
    padding: 1rem;
  }

  .login-form-container {
    max-width: 100%;
  }

  .login-title {
    font-size: 1.5rem;
  }

  .login-subtitle {
    margin-bottom: 1.5rem;
  }

  .main-title {
    font-size: 1.8rem;
  }

  .slogan {
    font-size: 1rem;
    margin-bottom: 30px;
  }

  .feature-icon {
    width: 50px;
    height: 50px;
    font-size: 1.4rem;
  }
}
</style>
